home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
050
/
wc.arc
/
WC.C
next >
Wrap
Text File
|
1986-04-10
|
1KB
|
63 lines
/* Figure 1 - wc program */
/* wc.c - count words */
#include <stdio.h>
#define WORD 0
#define NOT_WORD 1
FILE *fd ;
main(argc,argv)
int argc ;
char *argv[] ;
{
int c ;
long wc ;
if( argc < 2 )
{ printf("\n no name");
exit(0) ;
}
fd = fopen(argv[1],"r"); /* open the file */
if( fd == 0 )
{ printf("\n can't open");
exit(0) ;
}
wc = 0 ;
while( skip(NOT_WORD) != EOF ) /* skip to beginning of next word */
{ wc++ ;
if( skip(WORD) == EOF ) /* skip to end of the word */
break ;
} ;
fclose(fd) ;
printf("\n %ld words",wc);
}
int check(c) /* classify a char as part of word or not */
{
c = c & 0x7f ;
if( (c == ' ')
|| (c == '\c')
|| (c == '\n')
|| (c == ',')
|| (c == '.')
|| (c == '(')
|| (c == ')') )
return( NOT_WORD ) ;
else return( WORD ) ;
}
int skip(skip_type) /* skip chars of skip_type in file fd */
int skip_type ;
{
int c ;
c = getc(fd) ;
while( (c != EOF) && (check(c) == skip_type ) )
{ c = getc(fd) ; } ;
return( c ) ;
}